package at.jaki.ubb.crawling.stlib.dataFinder;

import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import at.jaki.ubb.crawling.model.DataFinderResult;
import at.jaki.ubb.crawling.model.Target;
import at.jaki.ubb.crawling.stlib.utils.WebPageMinerUtils;

/**
 * Data finder that uses the online Babylon translate service to translate a
 * word
 * 
 * @author Attila Jakabfi
 * 
 */
@SuppressWarnings("unused")
public class BabylonTranslator extends SimpleHttpClient {

	public static final String INPUT_TYPE = "translate";
	public static final String TARGET_TYPE = "babTrans";

	private static final String URL = "http://translation.babylon.com/translate/babylon.php?v=1.0&q=%s&langpair=0%%7C47&callback=babylonTranslator.callback&context=babylon.0.47._babylon_api_response";
	private static final String ID_SUFFIX = "_babTrans";

	private static final String BABYLON_TRANSLATE_URL = "translation.babylon.com";
	private static final String BABYLON_TRANSLATE_PATH = "/translate/babylon.php";

	private static final Logger LOG = Logger.getLogger(BabylonTranslator.class);
	

	@Override
	public DataFinderResult<String, String> findData(Target<String> t) {
		String url = null;

		// babylon expects a not encoded url for some reason...
		/*
		 * try { url = WebPageMinerUtils.createURI(BABYLON_TRANSLATE_URL,
		 * BABYLON_TRANSLATE_PATH, getParams(t.getValue()));
		 * LOG.debug("Using URL " + url + " for translation"); } catch
		 * (URISyntaxException e) { LOG.error(e); return new
		 * DataFinderResult<String, String>(null, null); }
		 */
		url = String.format(URL, formatQuery(t.getValue()));
		LOG.debug("Using URL " + url + " for translation");

		String page = getPage(url);
		String trWord = getTrResult(page);
		String decoded = WebPageMinerUtils.unescapeUnicode(trWord);
		decoded = WebPageMinerUtils.removeDiacriticalMarks(decoded);
		LOG.info("translated " + t.getValue() + " to " + decoded);

		Target<String> t2 = new Target<String>(0.8D, decoded + ID_SUFFIX, TARGET_TYPE, decoded);

		return new DataFinderResult<String, String>(null, Collections.singletonList(t2));
	}

	private Object formatQuery(String value) {
		return value.trim().replaceAll(" ", "%20");
	}

	private String getTrResult(String page) {
		Pattern p = Pattern.compile("[{][\"]translatedText[\"][:][\"](.*)[\"][}]");
		Matcher matcher = p.matcher(page);
		if (matcher.find()) {
			return matcher.group(1);
		}
		return null;
	}

	private Map<String, String> getParams(String value) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("v", "1.0");
		map.put("q", value.trim());
		map.put("langpair", "0|47");
		map.put("callback", "babylonTranslator.callback");
		map.put("context", "babylon.0.47._babylon_api_response");
		return map;
	}

	@Override
	public void init() {
	}

	@Override
	public Collection<String> getTypes() {
		return Collections.singletonList(INPUT_TYPE);
	}

}
